home *** CD-ROM | disk | FTP | other *** search
/ PC Player 2004 May / pc player 2004-05.iso / Demos / FarCry / Data1.cab / _42F0ADECA0964C6EBABA56D31B76B26C < prev    next >
Encoding:
Text File  |  2004-01-06  |  3.4 KB  |  149 lines

  1. -- make sure the table is loaded once
  2. if (not MapCycle) then
  3.     MapCycle =
  4.     {
  5.         iCurrentMap = 0,
  6.         iNextMap = 1,
  7.     };
  8. end
  9.  
  10. -- load the map cycle from a plain text file
  11. -- each line is one map
  12. -- maps can be repeated
  13. -- maps are played in the order specified in the file
  14. function MapCycle:LoadFromFile(szFilename)
  15.     self.MapList = {};
  16.     self.iCurrentMap = 0;
  17.     self.iNextMap = 1;
  18.     
  19.     local Lines = ReadTableFromFile(szFilename, 1);
  20.     if (Lines) then
  21.     
  22.         local iSpace;
  23.         local szMapName;
  24.         local szGameType;
  25.         
  26.         for i, szLine in Lines do
  27.             if (strlen(szLine) > 0) then
  28.                 iSpace = strfind(szLine, " ", 1, 1);
  29.                 
  30.                 if (iSpace) then
  31.                     szMapName = strsub(szLine, 1, iSpace-1);
  32.                     szGameType = strsub(szLine, iSpace+1, -1);
  33.                 else
  34.                     szMapName = szLine;
  35.                 end
  36.                 
  37.                 local Map = {};
  38.                 
  39.                 Map.szName = szMapName;
  40.                 Map.szGameType = szGameType;
  41.                 
  42.                 tinsert(self.MapList, Map);
  43.             end
  44.         end
  45.         
  46.         self.MapList.n = nil;
  47.     end
  48. end
  49.  
  50. -- call this everytime map changed
  51. function MapCycle:OnMapChanged()
  52.  
  53.     if (not self:IsOk()) then
  54.         return
  55.     end
  56.     
  57.     if (strlower(getglobal("g_LevelName")) ~= strlower(self.MapList[self.iNextMap].szName)) then
  58.         return;
  59.     end
  60.  
  61.     self.iCurrentMap = self.iNextMap;
  62.     self.iNextMap = self.iCurrentMap + 1;
  63.     
  64.     if (self.MapList[self.iNextMap] == nil) then
  65.         self.iNextMap = 1;
  66.     end
  67.     
  68.     setglobal("gr_NextMap", self.MapList[self.iNextMap].szName);
  69. end
  70.  
  71. -- call this to know if a map cycling is possible or no
  72. function MapCycle:IsOk()
  73.     if (self.MapList and (count(self.MapList) > 0)) then
  74.         return 1;
  75.     end
  76.     
  77.     return nil;
  78. end
  79.  
  80. -- call this to know if the scores are being shown, as a result of map finished
  81. function MapCycle:IsShowingScores()
  82.     if (self.fFinishedTimer) then
  83.         return 1;
  84.     end
  85.     
  86.     return nil;
  87. end
  88.  
  89. -- call this when the map is finished
  90. -- this function should load nextmap, and update self
  91. function MapCycle:OnMapFinished()
  92.     -- print game statistics into console and log
  93.     MPStatistics:Print();
  94.     MPStatistics:Init();
  95.     --
  96.     printf("\002START MAPCYCLE TIMER: %g", _time);
  97.     GameRules:ForceScoreBoard(1);
  98.     self.fFinishedTimer = _time + 5;
  99. end
  100.  
  101. -- call this function every frame
  102. -- prolly from GameRules:OnUpdate()
  103. function MapCycle:Update()
  104.     if (self.fFinishedTimer and (_time > self.fFinishedTimer)) then
  105.         
  106.         printf("\002END MAPCYCLE TIMER: %g", _time);
  107.                     
  108.         self.fFinishedTimer = nil;
  109.  
  110.         if (not self:IsOk()) then
  111.             self.fRestartTimer = _time + 5;
  112.                 
  113.             return
  114.         end
  115.         
  116.         local Map = self.MapList[self.iNextMap];
  117.         
  118.         if (Map) then
  119.             GameRules:ChangeMap(Map.szName, Map.szGameType);
  120.         end
  121.     elseif (self.fRestartTimer) then        
  122.         if (_time > self.fRestartTimer) then
  123.             self.fRestartTimer = nil;
  124.             self.fRestartTimerSecond = nil;
  125.             
  126.             if tostring(getglobal("gr_PrewarOn"))~="0" then
  127.                 self:NewGameState(CGS_PREWAR);
  128.             else
  129.                 GameRules:DoRestart();
  130.             end
  131.         else
  132.             local fSecond = max(0, floor(self.fRestartTimer - _time));
  133.             
  134.             if (fSecond ~= self.fRestartTimerSecond and fSecond > 0) then
  135.                 self.fRestartTimerSecond = fSecond;
  136.                 
  137.                 Server:BroadcastText("@GameRestartingIn "..fSecond, 0.9);
  138.             end            
  139.         end
  140.     end    
  141. end
  142.  
  143. -----------------------------------------------------------------------------------
  144. -----------------------------------------------------------------------------------
  145.  
  146. -- load the map cycle file
  147. if (getglobal("sv_mapcyclefile")) then
  148.     MapCycle:LoadFromFile(getglobal("sv_mapcyclefile"));
  149. end